package com.gin.no_gay_alliance.fragment.user.tab;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.nga.exception.NgaClientException;
import com.gin.nga.exception.NgaException;
import com.gin.nga.response.field.TopicInfoSimple;
import com.gin.no_gay_alliance.adpter.topic.TopicItemRecentAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.Callback;
import com.gin.no_gay_alliance.databinding.FragmentUserCenterTabUserRecentTopic2Binding;
import com.gin.no_gay_alliance.viewmodel.TopicItem;
import com.gin.no_gay_alliance.viewmodel.UserInfoModel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import okhttp3.Call;

/**
 * 用户最近主题
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/7 11:45
 **/
public class UserRecentTopicTabFragment extends BaseUserCenterTabFragment<FragmentUserCenterTabUserRecentTopic2Binding> implements Callback<List<TopicInfoSimple>> {
    private TopicItemRecentAdapter adapter;

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        adapter = new TopicItemRecentAdapter(requireContext(), new ArrayList<>());
        b.tvWarning.setVisibility(View.GONE);
        b.recyclerView.setAdapter(adapter);
        b.swipeRefreshLayout.setOnRefreshListener(() -> sendRequest(true));
    }


    @Override
    public void onResume() {
        super.onResume();
        sendRequest(false);
    }

    /**
     * 请求成功回调
     *
     * @param data 数据
     */
    @Override
    public void onSuccess(List<TopicInfoSimple> data) {
        List<TopicItem> topicItems = data.stream().map(TopicItem::new).collect(Collectors.toList());
        ui(() -> {
            adapter.updateData(topicItems);
            b.swipeRefreshLayout.setRefreshing(false);
            b.tvWarning.setVisibility(View.GONE);
        });
    }

    /**
     * 请求之前执行的操作
     */
    @Override
    public void beforeRequest() {
        b.swipeRefreshLayout.setRefreshing(true);
    }

    /**
     * 请求失败回调(一般为网络访问错误)
     *
     * @param call call
     * @param e    错误
     */
    @Override
    public void onFailure(@NonNull Call call, @NonNull IOException e) {
        showWarning(e);
    }

    /**
     * 处理Nga异常
     *
     * @param e 异常
     */
    @Override
    public void handleException(NgaException e) {
        showWarning(e);
    }

    /**
     * 执行请求
     *
     * @param ignoreCache 是否忽略缓存
     */
    private void sendRequest(boolean ignoreCache) {
        MyApplication.app.managerFactory.getUserRecentTopicManager().request(ignoreCache, userInfoModel.userId, this);
    }

    /**
     * 显示报错提示，隐藏主题列表
     *
     * @param e 异常
     */
    private void showWarning(IOException e) {
        String msg = (e instanceof NgaClientException) ? ((NgaClientException) e).getReason().get(0) : e.getMessage();
        ui(() -> {
            b.swipeRefreshLayout.setRefreshing(false);
            b.tvWarning.setVisibility(View.VISIBLE);
            b.tvWarning.setText(msg);
        });
    }

    /**
     * 实例化
     *
     * @param userInfoModel 用户信息
     * @return Fragment
     */
    public static UserRecentTopicTabFragment newInstance(UserInfoModel userInfoModel) {
        UserRecentTopicTabFragment fragment = new UserRecentTopicTabFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_USER_INFO, userInfoModel);
        fragment.setArguments(args);
        return fragment;
    }
}
